package com.easyse.serviceuser.controller;

import com.easyse.model.DO.User;
import com.easyse.model.vo.ResultVO;
import com.easyse.serviceclient.clients.UserClient;
import com.easyse.serviceuser.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.OverridesAttribute;
import javax.validation.Valid;

/**
 * @author: zky
 * @date: 2022/10/05
 * @description:
 */
@RestController
@RequestMapping("/user")
@Api
public class UserController implements UserClient {

    @Autowired
    UserService userService;

    @GetMapping("/{id}")
    @Override
    public ResultVO finduser(@PathVariable("id") Long id) {
        ResultVO resultVO = ResultVO.success();
        resultVO.data("user",userService.getById(id));
        return resultVO;
    }

    //更改用户信息

    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public ResultVO updateuser (@Valid @RequestBody  User user, @PathVariable Integer id) {
        return userService.updateUser(user);
    }

    // 用户列表

    @GetMapping("/users/{pageNum}")
    public ResultVO updateuser (@PathVariable Integer pageNum) {

        return userService.getUserList(pageNum);
    }
    // 删除用户
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public ResultVO deleteUser (@PathVariable Integer id) {

        return userService.deleteUser(id);
    }
    // 添加用户
    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResultVO addUser (@RequestBody  User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        String phonenumber = user.getPhonenumber();
        if(ObjectUtils.isEmpty(username)
        || ObjectUtils.isEmpty(password)
        || ObjectUtils.isEmpty(phonenumber)
        ){
          return ResultVO.failure("用户信息不完整!请补全用户信息!");
        }
        return userService.addUser(user);
    }
    //TODO 搜索用户
    @GetMapping(value = "/search")
    public ResultVO searchUser(String string){
        return userService.searchUserlikeName(string);
    }

}
